/**
 * 用户相关操作的接口
 * 
 */
const express = require("express")
const router = express.Router()
const pool = require("../../db/db.js")
// 引入发送邮件的模块
const nodemailer = require("nodemailer")
const { generateTokn } = require("../../auth/jwt.js")
const Response = require("../../utils/ResponseData.js")
// 引入随机数工具类
const random = require("../../utils/random.js")

// 校验邮箱唯一性
router.post("/checkEmail", (req, res, next) => {
    let { email } = req.body
    let sql = `select * from user where u_email = ?`
    pool.query(sql, [email], (err, data) => {
        if (err) {
            console.log("校验邮箱唯一性错误", err);
            next(err)
            return
        }
        if (data.length == 0) {
            res.send(Response.success(null, "邮箱号可以使用"))
        } else {
            res.send(Response.error("该邮箱已被注册"))
        }

    })

})

// 发送邮箱验证码接口
router.post("/sendCode", (req, res, next) => {
    // 生成4位随机数验证码1000-9999
    let code = random(1000, 9999)
    // 获取传递进来的邮箱号
    let { email } = req.body
    let sql = `insert into user_code values(?,?)`
    pool.query(sql, [email, code], (err, data) => {
        if (err) {
            console.log("发送验证码错误", err);
            next(err)
            return
        }
        // 发送邮件
        // 需要制作一个邮件模板
        let transporter = nodemailer.createTransport({
            // service:"qq"
            host: "smtp.163.com",
            secureConnection: true,
            port: 465,
            auth: {
                user: "17792500936@163.com",
                pass: "ORUYRPLNUVTFQZYP"
            },
        })
        let options = {
            from: "17792500936@163.com",//发送者
            to: `${email},17792500936@163.com`,//接收者,用户的注册邮箱
            // 邮箱标题
            subject: "OnePet官方",
            // 邮件内容模板
            html: `
            <!DOCTYPE html>
            <html lang="en">
            
            <head>
                <meta charset="UTF-8">
                <meta http-equiv="X-UA-Compatible" content="IE=edge">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <title>Document</title>
            </head>
            <body>
                <div>
                    <h3>
                        尊敬的用户你好:
                    </h3>
                    <p>欢迎您注册OnePet!您的验证码是${code}!请注意查收!</p>
                </div>
            </body>
            </html>
            `
        }
        // 发送邮件
        transporter.sendMail(options, (err, msg) => {
            if (err) {
                console.log("发送失败", err);
                res.send(Response.error(err))
            } else {
                res.send(Response.success(null, msg))
            }
        })
    })
})
// 检验验证码
router.post("/checkCode", (req, res, next) => {
    // 获取前端传递的邮箱,验证码
    let { email, code } = req.body
    console.log(req.body);
    console.log(email, code);
    let sql = `select * from user_code where u_email=? and u_code=?`
    // pool.query(sql, [email, code], (err, data) => {
    //     console.log(data);
    //     // console.log(res);
    // })
    pool.query(sql, [email, code], (err, data) => {
        console.log(data);
        if (err) {
            console.log("校验验证码", err);
            next(err)
            return
        }
        if (data.length == 0) {
            res.send(Response.error("验证码错误"))
        } else {
            res.send(Response.success(null, "验证码正确"))
        }
    })
})
// 用户注册
router.post("/register", (req, res, next) => {
    console.log(req.body);
    let { u_name, u_email, u_pwd } = req.body
    let sql = `insert into user(u_name,u_email,u_pwd) values(?,?,?)`
    pool.query(sql, [u_name, u_email, u_pwd], (err, data) => {
        console.log(data);
        if (err) {
            console.log("注册错误", err);
            next(err)
            return
        }
        if (data.affectedRows > 0) {
            res.send(Response.success(null, "注册成功"))
        } else {
            res.send(Response.error("注册失败"))
        }
    })
})
module.exports = router